package debuggen;

import java.io.IOException;
import java.util.logging.*;


/**
 * LogWriter
 * Write info to a log file
 * @author Danny
 */

public class LogWriter {
	// File handler
	private FileHandler handler;
	// Logger
	private Logger logger;
	
	/**
	 * constructor
	 */
	public LogWriter()
	{
	}
	/**
	 * Open
	 * start possibility to log something
	 */
	private void open()
	{
		try 
		{
			// Create new handler
			handler = new FileHandler("debug.log", true);
			// Logger
			logger = Logger.getLogger("Debug IDP");
			logger.addHandler(handler);
		}catch(IOException exc)
		{
			
		}
	}
	/**
	 * writeMessage
	 * Write message to log file.
	 * @param p_strMessage
	 */
	public void writeMessage(String p_strMessage)
	{
		// Open log file
		open();
		
		// Write information to log
		LogRecord record = new LogRecord(Level.INFO, p_strMessage);
		handler.publish(record);
		
		// Close log file
		close();
	}
	/**
	 * writeMessage
	 * Write message to log file incl message level.
	 * @param p_strMessage
	 * @param p_Level
	 */
	public void writeMessage(String p_strMessage, Level p_Level)
	{
		// Open log file
		open();
		
		// Write information to log
		LogRecord record = new LogRecord(p_Level, p_strMessage);
		handler.publish(record);
		
		// Close log file
		close();
	}
	
	/**
	 * writeMessage
	 * Write message to log file incl message level.
	 * @param p_strMessage
	 * @param p_Level
	 * @param p_excException
	 */
	public void writeMessage(String p_strMessage, Level p_Level, Exception p_excEcxception)
	{
		// Open log file
		open();
		
		// Write information to log
		LogRecord record = new LogRecord(p_Level, p_strMessage + p_excEcxception.toString());
		handler.publish(record);
		
		// Close log file
		close();
	}
	
	/**
	 * close
	 * Close log
	 */
	private void close()
	{
		logger = null;
		handler.close();
	}
	/**
	 * Testdriver
	 * @param args
	 */
	public static void main(String[] args){
		LogWriter w = new LogWriter();
		w.writeMessage("Even een logginkje");
	}

}
